<!DOCTYPE html>
<html lang="en">
<title>兑换码新增记录</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title overflow">
                    <h5 class="pull-left">兑换码新增记录</h5>
                    <button type='button' id="btn_dow" class='btn btn-sm btn-primary pull-right' style="margin-left: 10px;">导出</button>
                    <a href="to_add_step1" class="btn btn-sm btn-primary pull-right" style="margin-left: 10px; ">新增储值卡</a>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <form class="form-inline" action="to_cashcard_list" role="form" id="form" method="post" accept-charset="UTF-8">
                            <div class="form-group">
                                <label for="storeName">批次ID：</label>
                                <input class="form-control" type="text" id="id" name="id" value="${id!''}"/>
                            </div>
                            <div class="form-group">
                                <label for="storeName1">名称：</label>
                                <input class="form-control" type="text" id="cashcardName" name="cashcardName" value="${cashcardName!''}"/>
                            </div>
                            <div class="form-group">
                                <label>状态：</label>
                                <select class="form-control" id="cashcardStatus" name="cashcardStatus">
                                    <option value="0"
                                    <#if cashcardStatus = 0> selected="selected"</#if>
                                    >全部</option>
                                    <option value="1"
                                    <#if cashcardStatus = 1> selected="selected"</#if>
                                    >未开始</option>
                                    <option value="2"
                                    <#if cashcardStatus = 2> selected="selected"</#if>
                                    >生效中</option>
                                    <option value="3"
                                    <#if cashcardStatus = 3> selected="selected"</#if>
                                    >已过期</option>
                                    <option value="4"
                                    <#if cashcardStatus = 4> selected="selected"</#if>
                                    >已作废</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>操作人：</label>
                                <select class="form-control" id="checkName" name="checkName">
                                    <option value="all">全部</option>
                                    <#if checkList??>
                                        <#list checkList as check>
                                            <option value="${check.name!}"
                                            <#if check.name==checkName> selected="selected"</#if>
                                            >${check.name!}</option>
                                        </#list>
                                    </#if>

                                </select>
                            </div>
                            <input type="hidden" id="pageNo" name="pageNo" value="${pageNo!}">
                            <button type="submit" class="btn btn-outline btn-primary" style="margin-top: 5px">确认</button>
                        </form>
                        <hr style="width: 100%">
                        <table class="table table-hover table-center dataTables-example">
                            <thead>
                            <tr>
                                <th>批次ID</th>
                                <th>名称</th>
                                <th>兑换码</th>
                                <th>有效期</th>
                                <th>创建人</th>
                                <th>验证人</th>
                                <th>新增时间</th>
                                <th>生成数量</th>
                                <th>兑换数量</th>
                                <th>发放金额</th>
                                <th>已兑人数</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <#if list??>
                                <#list list as list>
                                    <tr>
                                        <td>${list.id!}</td>
                                        <td>${list.cashcardName!}</td>
                                        <td>${list.cashcardCode!'随机生成'}</td>
                                        <td>
                                            <#if list.changeMode = 1>
                                                不限制
                                            </#if>
                                            <#if list.changeMode = 2>
                                                ${list.changeBeginDate?date}</br>${list.changeEndDate?date}
                                            </#if>

                                        </td>
                                        <td>${list.createBy!}</td>
                                        <td>${list.checkName!}</td>
                                        <td>${list.createTime?date}</td>
                                        <td>${list.maxCount!}</td>
                                        <td>
                                            <#if list.isOneVote = 0>
                                                每人可兑换多个码
                                            </#if>
                                            <#if list.isOneVote = 1>
                                                每人限制兑换一个码
                                            </#if>
                                        </td>
                                        <td>${list.cashcardAmt!}</td>
                                        <td>${list.getedCount!'0'}</td>
                                        <td>
                                            <#if list.cashcardStatus = 1>
                                                未开始
                                            </#if>
                                            <#if list.cashcardStatus = 2>
                                                生效中
                                            </#if>
                                            <#if list.cashcardStatus = 3>
                                                已过期
                                            </#if>
                                            <#if list.cashcardStatus = 4>
                                                已作废
                                            </#if>
                                        </td>
                                        <td>

                                            <a href="to_cashcard_detail_view?cashcard_id=${list.id!}" class="btn btn-default"><i class="fa fa-folder"></i> 查看</a>
                                            <#if list.cashcardStatus  lt 3>
                                                <a href="to_add_step1?id=${list.id!}" class="btn btn-default"><i class="fa fa-pencil"></i> 编辑</a>
                                            </#if>
                                            <#if list.maxCount gt 0>
                                                <#if list.cashcardStatus lt 3>
                                                    <!-- <button class="btn btn-default btn-sm" onclick="downloadCashcardDetail('${list.id!}')"><i class="fa fa-trash"></i>下载</button> -->
                                                    <button type="button" class="btn btn-default btn-sm" onclick="downloadCashcardDetail('${list.id!}')">
                                                        <span class="glyphicon glyphicon-download"></span> 下载
                                                    </button>
                                                </#if>
                                            </#if>
                                            <#if list.cashcardStatus lt 3>
                                                <button class="btn btn-default" onclick="cashCardDel('${list.id!}')"><i class="fa fa-trash"></i> 余卡作废</button>
                                            </#if>
                                            <!-- <a href="javascript:;" class="btn btn-default" ><i class="fa fa-trash-o"></i></a> -->

                                            <#if list.getMode = 2 && list.cashcardStatus = 2>
                                                <button class="btn btn-default" onclick="downloadImg('${list.id!}')"><i class="fa fa-arrow-circle-down"></i> 生成二维码</button>
                                            </#if>
                                        </td>
                                    </tr>
                                </#list>
                            </#if>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="ibox-footer overflow">
                    <h4 class="pull-left">合计：${pageCount!}位储值卡用户</h4>
                    <div id="copot-page" class="pull-right"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<div style="display: none;" id="testDiv">

</div>
<script src="${baseUrl}js/main.js"></script>
<script src="${baseUrl}js/channel/jquery.qrcode.min.js"></script>
<script>
    $(document).ready(function () {
        //导出excel
        /* ExampleFile("用户excel"); */
        //分页
        new Page({
            parent: $("#copot-page"),
            nowPage: ${pageNo!},
            pageSize: 10,
            totalCount: ${pageCount!},
            setting: {
                defaultPage: 7,//默认显示可点击分页的个数
                firstPageText: "首页",
                prevPageText: "上一页",
                nextPageText: "下一页",
                lastPageText: "尾页"
            },
            callback: function (nowPage, totalPage) {
                $("#pageNo").val(nowPage);
                $("#form").submit();
            }
        });
        /**
         * 下载优惠券
         *
         * @param id
         */
        $("#btn_dow").click(function () {
            downloadCoupon();
        });

    });
    function downloadCoupon() {
        $.post("${baseUrl}system/cashcard/download_cashcard", function (res) {
            res = eval("(" + res + ")");
            if (res && res.data.result == "success") {
                swal("成功!", '文件下载成功！', "success");
                window.location = "${baseUrl}system/cashcard/download_file?fileName=" + res.data.fileName;
            } else {
                swal("错误!", res.data.msg, "error");
            }
        });
    }

    function downloadCashcardDetail(id) {
        $.post("${baseUrl}system/cashcard/download_detail?cashcardId=" + id, function (res) {
            res = eval("(" + res + ")");
            if (res && res.data.result == "success") {
                swal("成功!", '文件下载成功！', "success");
                window.location = "${baseUrl}system/cashcard/download_file?fileName=" + res.data.fileName;
            } else {
                swal("错误!", res.data.msg, "error");
            }
        });
    }
    // 余卡作废
    function cashCardDel(id) {
        swal({
            title: "确定要删除",
            text: "删除之后剩余未领取的也将作废！！！",
            type: "warning",
            showCancelButton: true,
            cancelButtonText: "容我三思！",
            confirmButtonText: "确定!",
        }).then(function () {
            $.ajax({
                url: 'cashcard_del',
                data: {
                    "cashCardId": id
                },
                type: 'post',
                cache: false,
                dataType: 'json',
                success: function (datas) {
                    var data = datas.data;
                    if (data == -1) {
                        swal("删除失败!", "", "error");
                        return;
                    } else {
                        swal("删除成功!", "", "success");
                        history.go(0);
                    }

                },
                error: function () {
                    swal("删除失败!", "", "error");
                }
            });
        }, function () {

        });

    }

    function downloadImg(id, fileName) {
        new Promise(function (resolve) {
            $.post("${baseUrl}system/cashcard/download_detail?cashcardId=" + id).done(function () {
                $.get("${baseUrl}system/cashcard/short_url?cardId=" + id, function (result) {
                    data = eval('(' + result + ')');
                    var shortUrl = data.data

                    fileName = "card_qr.jpg";
                    $("#testDiv").empty();
                    var html = '<div id="qrcode"></div><a id="download" download="qrcode.jpg"></a><button id="save">save</button>';
                    $("#testDiv").append(html);
                    console.log(shortUrl)
                    jQuery('#qrcode').qrcode({render: "canvas", text: shortUrl, width: 500, height: 500, background: "#ffffff", foreground: "#000000"});
                    var canvas = $('#qrcode').find("canvas").get(0);
                    var urlnew = canvas.toDataURL(shortUrl);
                    $("#download").attr('download', fileName).click();
                    $("#download").attr('href', "").click();
                    $("#download").attr('href', urlnew).get(0).click();
                    return false;
                })
            })
        })
    }
</script>
</@body.page>
</html>